package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.Gwai760;
import cn.e4j.modular.crm.mapper.Gwai760Mapper;
import cn.e4j.modular.system.mapper.SysUserMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-02-02
 */
@Service
public class Gwai760ServiceImpl extends ServiceImpl<Gwai760Mapper, Gwai760> {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> gwdaDtos = new ArrayList<>();
        if (!dto.getString("COMPANY").equals("") && !dto.getString("TI003_START").equals("")) {
            if (dto.getString("UDF12_J").equals("N")) {
                dto.put("status", "2");
            } else if (dto.getString("UDF12_J").equals("Y")) {
                dto.put("status", "1");
            }
            gwdaDtos = this.baseMapper.listPage(page, dto);
        }
        return gwdaDtos;
    }

    @Transactional
    public void refund(List<Gwai760> gwdaTList, Dto info) {
        //ShiroUser user = ShiroKit.getUser();
        for (Gwai760 gwai760 : gwdaTList) {
            Dto dto = Dtos.newDto();
            dto.put("UDF12", "Y");
            dto.put("UDF09", "Y");
            dto.put("UDF11", info == null ? null : info.getString("BIZ_CODE"));
            dto.put("UDF10", DateUtils.formatDate(new Date(), "yyyyMMdd"));
            dto.put("UDF07", DateUtils.formatDate(new Date(), "yyyyMMdd"));
            dto.put("TJ002", gwai760.getTI002());
            dto.put("COMPANY", gwai760.getCOMPANY());
            this.baseMapper.refundJ(dto);
            this.baseMapper.refundI(dto);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getName(List<Map<String, Object>> result) {
        for(int i = 0; i < result.size(); i ++){
            if(!result.get(i).get("UDF11").equals("")) {
                Dto dto = sysUserMapper.getUser(result.get(i).get("UDF11").toString());
                if (dto != null) {
                    result.get(i).put("UDF11", dto.getString("NAME"));
                }
            }
        }
       return result;
    }

    @Transactional(readOnly = true)
    public List<Dto> getListData(Dto dto) {
        List<Dto> dtos = this.baseMapper.listData(dto);
        return dtos;
    }
}
